package com.pti.usermanager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pti.usermanager.dao.api.UserDao;
import com.pti.usermanager.dao.po.UserPo;
import com.pti.usermanager.enums.ConstantCode;
import com.pti.usermanager.service.api.UserService;
import com.pti.usermanager.web.dto.request.DeleteUserRequest;
import com.pti.usermanager.web.dto.request.SaveUserRequest;
import com.pti.usermanager.web.dto.request.UpdateUserRequest;
import com.pti.usermanager.web.dto.response.ResultResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: UserServiceImpl
 * @Author: liumengyao
 * @Description: 用户业务实现层
 * @Date: 2019/1/16
 */
@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LogManager.getLogger(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;



    /**
     * @Author liumengyao
     * @Description 分页模糊查询用户
     * @Date 2019/1/17
     */
    @Override
    public PageInfo<UserPo> findAll(String loginNm,String usrNm, Integer page, Integer pageSize) {

        logger.debug("usrNm:{} page:{} pageSize:{}",usrNm,page,pageSize);
        //pageHelper工具分页
        PageHelper.startPage(page,pageSize);
        List<UserPo> userPoList = userDao.findAll(loginNm,usrNm);
        PageInfo<UserPo> pageInfo = new PageInfo<>(userPoList);
        return pageInfo;
    }


    /**
     * @Author liumengyao
     * @Description 根据用户id查询用户
     * @Date 2019/1/27
     */
    @Override
    public ResultResponse findAllByUsrId(Long usrId) {

        logger.debug("usrId:{}",usrId);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(userDao.findAllByUsrId(usrId));
        return resultResponse;
    }

    /**
     * @Author liumengyao
     * @Description 添加用户
     * @Date 2019/1/17
     */
    @Override
    public ResultResponse saveUser(SaveUserRequest saveUserRequest) {

        logger.debug("saveUserRequest:{}",saveUserRequest.toString());

        ResultResponse resultResponse = new ResultResponse();
        try {
            userDao.saveUser(saveUserRequest);
            return resultResponse;
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }


   /**
    * @Author liumengyao
    * @Description 更新用户
    * @Date 2019/1/17
    */
    @Override
    public ResultResponse updateUser(UpdateUserRequest updateUserRequest) {

        logger.debug("updateUserRequest:{}",updateUserRequest.toString());

        ResultResponse resultResponse = new ResultResponse();

        try{
            userDao.updateUser(updateUserRequest);
            return resultResponse;
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }

    /**
     * @Author liumengyao
     * @Description 删除用户
     * @Date 2019/1/17
     */
    @Override
    public ResultResponse delUser(DeleteUserRequest deleteUserRequest) {

        logger.debug("deleteUserRequest:{}",deleteUserRequest.toString());

        ResultResponse resultResponse = new ResultResponse();

        try{
            for(Long usrId:deleteUserRequest.getUsrIds()){
                userDao.delUser(usrId);
            }
            return resultResponse;
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }
}
